#!/bin/bash
#需要变更的服务，有多个服务需要变更就填多个，中间用","隔开
#input_param="rca-eureka,rca-service,rca-gateway,rca-auth,rca-comm"
input_param=$1
#ecex_type:start ; stop ; deploy ; restart
exec_type=$2

#应用备份路径
back_path="/home/ap/cloudapp/backup"
#jar包的是否需要解压标志位
unzip_jar_flay="yes"

#全量配置列表 "arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8"
#arg1:服务的名称
#arg2:版本包的路径
#arg3:应用部署的路径
#arg4:应用启动脚本（启动脚本需要带参数的可以放在脚本后，中间用":"隔开）
#arg5:应用停止脚本（停止脚本需要带参数的可以放在脚本后，中间用":"隔开）
#arg6:应用部署脚本（部署脚本需要带参数的可以放在脚本后，中间用":"隔开）
#arg7:应用部署模式（all：全量，add：增量）
#arg8:应用部署的服务器IP（IP之间用":"隔开）
#说明：novalue表示该参数没有值

tasks_list=(
"rca-eureka /home/ap/cloudapp/version/rca-eureka.jar /home/ap/cloudapp/cloud-app/rca-eureka /home/ap/nas/tools/start_eureka.sh:prod /home/ap/nas/tools/stop_eureka.sh novalue all 10.4.9.2:10.4.9.7
")

curr_time=$(date +"%Y%m%d%H%M%S")
local_host=$(echo $(ifconfig -a | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:") | awk '{print $1}')

#用途：检查配置参数
#使用方法：check_config arg1 arg2 arg3 arg4 arg5 arg6
#arg1:应用名称
#arg2:应用更新包路径
#arg3:应用部署路径
#arg4:应用启动脚本
#arg5:应用停止脚本
#arg6:应用部署脚本
check_config() {
    [[ $# != 6 ]] && echo "[Error]: check_config:Input parameter must be six!" && exit 1
    app_name=$1
    version_path=$2
    app_path=$3
    app_start=$4
    app_stop=$5
    app_deploy=$6
    [[ ! (-d $version_path || -f $version_path) ]] && echo "[Error]: $app_name, Please check the version_path!" && exit 1
    [[ ! -d $app_path ]] && echo "[Error]: $app_name, Please check the app_path!" && exit 1
    [[ x"$app_start" != x"novalue" && ! -f ${app_start%%:*} ]] && echo "[Error]: $app_name, Please check the app_start!" && exit 1
    [[ x"$app_stop" != x"novalue" && ! -f ${app_stop%%:*} ]] && echo "[Error]: $app_name, Please check the app_stop!" && exit 1
    [[ x"$app_deploy" != x"novalue" && ! -f ${app_deploy%%:*} ]] && echo "[Error]: $app_name, Please check the app_deploy!" && exit 1
}

#用途：应用备份
#使用方法：app_backup arg1 arg2 arg3
#arg1:应用备份路径
#arg2:应用部署路径
#arg3:应用部署方式（all：全量，add：增量）
app_backup() {
    if [ $# != 3 ]; then
        echo "app_backup:Input parameter must be three!"
        exit 1
    fi
    back_path=$1
    app_path=$2
    app_deploy=$3

    if [ ! -d $back_path/$curr_time ]; then
        mkdir -p $back_path/$curr_time
    fi
    if [ x"$app_deploy" = x"all" ]; then
        mv $app_path $back_path/$curr_time
        mkdir -p $app_path
    else
        cp -R $app_path $back_path/$curr_time
    fi
}

#用途：应用停止
#使用方法：app_stop arg1 arg2
#arg1:应用服务名称
#arg2:应用停止脚本路径
app_stop() {
    if [ $# != 2 ]; then
        echo "[Error]: app_stop, Input parameter must be two!"
        exit 1
    fi
    app_name=$1
    app_stop=$2

    if [ -f ${app_stop%%:*} ]; then
        process_num=$(ps -ef | grep $app_name | grep -v grep | wc -l)
        if [ $process_num -eq 0 ]; then
            echo "[Info]: $app_name, The process is not exist!"
        else
            sh ${app_stop//:/ } >/dev/null
            sleep 3
            process_num=$(ps -ef | grep $app_name | grep -v grep | wc -l)
            if [ $process_num -eq 0 ]; then
                echo "[Info]: $app_name stop successfully!"
            else
                echo "[Error]: $app_name stop failed!"
                #exit 1
            fi
        fi
    fi
}

#用途：应用备份更新
#使用方法：app_deploy arg1 arg2 arg3 arg4 arg5 arg6
#arg1:应用名称
#arg2:应用更新包路径
#arg3:应用部署路径
#arg4:应用备份路径
#arg5:应用部署脚本
#arg6:应用部署模式（all：全量，add：增量）
app_deploy() {
    if [ $# != 6 ]; then
        echo "[Error]: app_update, Input parameter must be six!"
        exit 1
    fi
    app_name=$1
    version_path=$2
    app_path=$3
    back_path=$4
    app_deploy=$5
    deploy_mode=$6

    if [ -f ${app_deploy%%:*} ]; then
        sh ${app_deploy//:/ } >/dev/null
    else
        if [ -d $version_path ]; then
            app_backup $back_path $app_path $deploy_mode
            cp -R $version_path/* $app_path
        else
            app_backup $back_path $app_path $deploy_mode
            cp -R $version_path $app_path
            cd $app_path || exit
            if [ x"${version_path##*.}" = x"jar" ]; then
                [[ x"$unzip_jar_flay" = x"yes" ]] && jar -xf $app_path/${version_path##*/}
            elif [ x"${version_path##*.}" = x"zip" ] || [ x"${version_path##*.}" = x"war" ]; then
                unzip $app_path/${version_path##*/}
            elif [ x"${version_path##*.}" = x"tgz" ]; then
                tar -zxf $app_path/${version_path##*/}
            else
                echo "[Error]: Unsupported file format!"
                #exit 1
            fi
        fi
    fi

    if [ -f $HOME/extend_shell.sh ]; then
        sh $HOME/extend_shell.sh >$HOME/extend_shell.log
    fi

    echo "[Info]: $app_name update successfully!"
}

#用途：应用启动
#使用方法：app_start arg1 arg2
#arg1:应用服务名称
#arg2:应用启动脚本路径
app_start() {
    if [ $# != 2 ]; then
        echo "[Error]: app_start, Input parameter must be two!"
        exit 1
    fi
    app_name=$1
    app_start=$2

    if [ -f ${app_start%%:*} ]; then
        process_num=$(ps -ef | grep $app_name | grep -v grep | wc -l)
        if [ $process_num -eq 0 ]; then
            sh ${app_start//:/ } >/dev/null
            sleep 3
            process_num=$(ps -ef | grep $app_name | grep -v grep | wc -l)
            if [ $process_num -gt 0 ]; then
                echo "[Info]: $app_name start successully!"
            else
                echo "[Error]: $app_name start failed!"
                #exit 1
            fi
        else
            echo "[Info]: $app_name, The process is exist!"
        fi
    fi
}

update_service=$(echo ${input_param//,/ })
for each_service in $update_service; do
    echo -e "${tasks_list}" | grep -E "${each_service}\s" | while read line; do
        read -r app_name version_path app_path app_start app_stop app_deploy deploy_mode ip_list <<<$line
        if [ x"$app_name" = x"$each_service" ]; then
            if [[ $(echo -e "${ip_list//:/ }" | grep -w "$local_host" | wc -l) -gt 0 ]]; then
                echo "----------$local_host:$app_name----------"
                case $exec_type in
                "deploy")
                    check_config $app_name $version_path $app_path $app_start $app_stop $app_deploy
                    app_stop $app_name $app_stop
                    app_deploy $app_name $version_path $app_path $back_path $app_deploy $deploy_mode
                    app_start $app_name $app_start
                    ;;
                "start")
                    app_start $app_name $app_start
                    ;;
                "stop")
                    app_stop $app_name $app_stop
                    ;;
                "restart")
                    app_stop $app_name $app_stop
                    sleep 3
                    app_start $app_name $app_start
                    ;;
                *)
                    echo "usage: start ; stop ; restart : deploy !"
                    ;;
                esac
            fi
        fi
    done
done